package com.sgb.zhaocai.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.OfferListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.fragment.OfferListFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

public class OfferListFragmentModel extends ViewModel {


    private boolean isLastPage;
    private String longitudes ="";
    private String latitudes = "";

    /**
     * 报价列表查询
     * @param fragment
     * @param requestBean
     */
    public void RecruitList(OfferListFragment fragment, RequestBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getRecruitList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<OfferListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<OfferListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    fragment.setRecruitList(data.getData().getList(),isLastPage);
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    /**
     * 报价详情
     * @param fragment
     * @param id
     * @param type
     */
    public void getWebH5(OfferListFragment fragment, String id, Integer type) {
        longitudes = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        latitudes = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
        if (TextUtils.isEmpty(longitudes)) {
            longitudes = "0";
        }
        if (TextUtils.isEmpty(latitudes)) {
            latitudes = "0";
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("MyofferDetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id + "&type=" + type + "&localLat=" + latitudes+"&localLng=" +longitudes ;
                    BaseApi.startWeb(fragment.getContext(), webUrl);
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
}
